package Netty.protobuf;

import Netty.protobuf.protoClass.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

public class NettyClientHandler extends SimpleChannelInboundHandler<Message.MessageProto> {


    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println("netty client 启动...");
        Message.MessageProto messageProto = Message.MessageProto.newBuilder().setId(1).setContent("hello netty server").build();
        ctx.channel().writeAndFlush(messageProto);
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message.MessageProto msg) throws Exception {
        System.out.println("netty 服务器响应信息:" +  msg.toString());
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

}